package org.wzl.service.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.wzl.service._do.BlogCategory;
import org.wzl.service.convert.BlogCategoryMapperStruct;
import org.wzl.service.dto.BlogCategorySaveDTO;
import org.wzl.service.exception.BusinessException;
import org.wzl.service.service.BlogCategoryService;
import org.wzl.service.mapper.BlogCategoryMapper;
import org.springframework.stereotype.Service;
import org.wzl.service.service.UserInfoService;
import org.wzl.service.vo.BlogCategoryVO;

import java.util.List;

/**
* @author 卫志龙
* @description 针对表【t_blog_category】的数据库操作Service实现
* @createDate 2024-06-30 18:26:46
*/
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory>
    implements BlogCategoryService{

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private BlogCategoryMapperStruct blogCategoryMapperStruct;

    @Override
    public void saveBlogCategory(BlogCategorySaveDTO blogCategorySaveDTO) {
        String userId = userInfoService.getCurrentUserId();
        if (count(Wrappers.<BlogCategory>lambdaQuery()
                .eq(BlogCategory::getCategoryName, blogCategorySaveDTO.getCategoryName())
                .eq(BlogCategory::getUserId, userId)) > 0) {
            throw new BusinessException("已添加过相同分类");
        }

        BlogCategory blogCategory = blogCategoryMapperStruct.toDO(blogCategorySaveDTO);
        save(blogCategory);
    }

    @Override
    public List<BlogCategoryVO> getAll() {
        return list(Wrappers.<BlogCategory>lambdaQuery()
                .eq(BlogCategory::getUserId, userInfoService.getCurrentUserId()))
                .stream()
                .map(blogCategoryMapperStruct::toVO)
                .toList();
    }

}




